package com.yhq.basic.algorithm.array;

import java.util.*;

/**
 * @author hqyin
 * @date 2023/7/5 5:18 下午
 */
public class TestArrayList {
    public static void main(String[] args) {
        //测试自定义MyArrayList
        //test_myArrayList();

        //测试 modCount
        //test_foreach();
        //test_iterator();

        //test listIterator
//        test_listIterator();
        List<String> list = new Vector<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String tmp = iter.next();
            System.out.println(tmp);
            if (tmp.equals("1")) {
                list.remove("1");
            }
        }

    }

    private static void test_listIterator() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        System.out.println(list);
        Iterator<Integer> iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer obj = iterator.next();
            if(2 == obj) {
                iterator.remove();
            }
        }
        System.out.println(list);

        ListIterator<Integer> listIterator = list.listIterator(2);
        while (listIterator.hasPrevious()) {
            int index = listIterator.previousIndex();

            System.out.println("下标-》"+index+":"+listIterator.previous());
        }
    }

    private static void test_iterator() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");
        Iterator<String> iter = list.iterator();
        while (iter.hasNext()) {
            String tmp = iter.next();
            System.out.println(tmp);
            if (tmp.equals("1")) {
                list.remove("1");
            }
        }
    }

    private static void test_foreach() {
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.forEach(e->{
            if(e==2) {
                list.remove(e);
            }else {
                System.out.println(e);
            }
        });
    }

    private static void test_myArrayList() {
        MyArrayList<Integer> list = new MyArrayList<>();
        list.add(1);
        list.add(2);
        System.out.println(list);
        System.out.println(list.get(0));
        System.out.println(list.size());

        list.remove(0);
        System.out.println(list);
        list.set(0,5);
        System.out.println(list);
        //如何使用stream
        System.out.println(list.stream().filter(e->e != null).mapToInt(e->e).max().orElse(0));;
        list.clear();
        System.out.println(list);
    }
}
